import React, { Component } from 'react';
import { Button, Modal, message } from 'antd';
import { connect } from 'dva';

const { confirm } = Modal;

@connect(state => state)
export default class DeleteBtn extends Component {

  del = () => {
    const { namespace, item } = this.props;
    const currState = this.props[namespace];
    const { selectedRowKeys } = currState;
    if (!selectedRowKeys || !selectedRowKeys.length) {
      message.warn(`请选择要删除的${item}`);
      return;
    }
    confirm({
      title: '删除',
      content: `确定要删除选中的${item}吗`,
      onOk: () => this.confirm(),
    });
  }

  confirm = () => {
    const { dispatch, namespace } = this.props;
    const currState = this.props[namespace];
    const { selectedRowKeys } = currState;
    dispatch({
      type: `${namespace}/multiDelete`,
      ids: selectedRowKeys,
    });
  }

  render() {
    return (
      <Button
        type="primary"
        style={{ marginRight: '1em' }}
        icon="delete"
        className="opt-but"
        onClick={this.del}
      >
        删除
			</Button>
    );
  }
}
